/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.coinche.client.business.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import org.jb.coinche.client.business.model.Card.Suit;
import org.jb.statemachine.client.AbstractState;

public class RoundTurn extends AbstractState implements GameState {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -312769390479189607L;
	private Round round;
	private Player currentPlayer;
	private HashMap< Player, Card > playedCards = new HashMap< Player, Card >();
	private HashMap< Player, ArrayList<Card> > playableCards = null;
	private Suit asked = null;

	/**
	 * 
	 */
	public Game getGame() {
		return getRound().getGame();
	}
	
	/**
	 * @param round the round to set
	 */
	public void setRound(Round round) {
		this.round = round;
	}

	/**
	 * @return the round
	 */
	public Round getRound() {
		return round;
	}

	/**
	 * @param currentPlayer the currentPlayer to set
	 */
	public void setCurrentPlayer(Player currentPlayer) {
		this.currentPlayer = currentPlayer;
	}

	/**
	 * @return the currentPlayer
	 */
	public Player getCurrentPlayer() {
		return currentPlayer;
	}
	
	/**
	 * 
	 * @return
	 */
	public Suit getAskedColor() {
		return asked;
	}
	
	/**
	 * 
	 * @param p
	 * @return
	 */
	public Card getPlayedCard( Player p )  {
		return playedCards.get( p );
	}
	
	/*
	 * 
	 */
	public boolean isFinished() {
		return playedCards.size() == 4;
	}
	
	/*
	 * 
	 */
	public void play( Player player, Card card ) {
		playedCards.put( player, card );
		
		if (asked == null)
			asked = card.getSuit();
		
		currentPlayer = round.getGame().getNextPlayer( player );
	}

	/**
	 * @param playableCards the playableCards to set
	 */
	public void setPlayableCards(HashMap< Player, ArrayList<Card> > playableCards) {
		this.playableCards = playableCards;
	}

	/**
	 * @return the playableCards
	 */
	public Collection<Card> getPlayableCards( Player p ) {
		return playableCards == null ? null : playableCards.get(p);
	}

	/**
	 * 
	 * @return
	 */
	public Collection<Card> getPlayedCards() {
		return playedCards.values();
	}
}
